package com.ctrip.corp.obt.shard.controller;

import com.ctrip.corp.obt.generic.core.context.RequestContext;
import com.ctrip.corp.obt.generic.core.http.RequestWrapper;
import com.ctrip.corp.obt.shard.core.DataPlaneSource;
import com.ctrip.corp.obt.shard.manager.DataPlaneSourceManager;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Set;


@Slf4j
@RestController
@RequiredArgsConstructor
@RequestMapping("/api/datasource")
public class DataPlaneSourceController {

    private final DataPlaneSourceManager dataPlaneSourceManager;

    @PostMapping("/load")
    public Set<String> reloadBySources() {
        HttpServletRequest request = RequestContext.getCurrentContext().getRequest();
        String tenantIds = request.getParameter("tenantIds");
        String source = request.getParameter("source");
        return dataPlaneSourceManager.loadBySources(new DataPlaneSource(tenantIds, source));
    }

    @GetMapping("/members")
    public Set<String> listSourceMembers() {
        return dataPlaneSourceManager.listSourceMembers();
    }

}
